package com.llsydn.a01androidh5;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author lilinshen
 * @title Java代码和H5代码互调
 * @description java互相调用js
 * @date 2018/10/19 13:51
 */
public class JavaJsMutualActivity extends Activity implements View.OnClickListener {
    private EditText etNumber;
    private EditText etPassword;
    private Button btnLogin;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java_js);
        findViews();
        initWebView();
    }


    /**
     * Find the Views in the layout<br />
     * <br />
     * Auto-created on 2018-10-19 13:51:23 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    private void findViews() {
        etNumber = (EditText) findViewById(R.id.et_number);
        etPassword = (EditText) findViewById(R.id.et_password);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
    }

    /**
     * Handle button click events<br />
     * <br />
     * Auto-created on 2018-10-19 13:51:23 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    @Override
    public void onClick(View v) {
        if (v == btnLogin) {
            // Handle clicks for btnLogin
            login();
        }
    }

    private void login() {
        //得到账号和密码
        String number = etNumber.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        //判断密码是否为空
        if (TextUtils.isEmpty(number) || TextUtils.isEmpty(password)) {
            Toast.makeText(JavaJsMutualActivity.this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
        } else {
            //登录（加载一个登录网页）
            login(number);
        }
    }

    private void login(String number) {
        //将number传入html页面中
        webView.loadUrl("javascript:javaCallJs(" + "'" + number + "'" + ")");
        setContentView(webView);
    }

    private void initWebView() {
        //加载网页H5,html,自定义浏览器
        webView = new WebView(this);
        WebSettings webSettings = webView.getSettings();
        //设置支持js代码
        webSettings.setJavaScriptEnabled(true);
        //不调起浏览器-自定义浏览器
        webView.setWebViewClient(new WebViewClient());

        //添加JavascriptInterface
        //以后js通过android字段，调用JavascriptInterface的任何方法。例如：window.Android.xxxx
        webView.addJavascriptInterface(new AndroidAndJsInterface(), "Android");

        //加载网页（网络或本地的）
        //webView.loadUrl("http://www.zhongshanou.cn/ddmh/portal/home/index.jhtml");
        webView.loadUrl("file:///android_asset/JavaAndJavaScriptCall.html");
    }

    /**
     * js调java
     */
    class AndroidAndJsInterface {
        /**
         * 将会被js调用
         */
        @JavascriptInterface
        public void showToast() {
            Toast.makeText(JavaJsMutualActivity.this, "我是android代码，我被js调用了", Toast.LENGTH_SHORT).show();
        }
    }

}
